<template>
    <h2 style="text-align: center ">借款登录</h2>
    <table border="1">
        <tr>
            <td>账号</td>
            <td>
                <input type="text" v-model="info.Uname">
            </td>
        </tr>
        <tr>
            <td>密码</td>
            <td>
                <input type="password" v-model="info.Upwd">
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="登录" @click="login()">
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
import {ref} from 'vue'
import {useRouter} from 'vue-router'
import axios from 'axios'
import { useStore } from '../store/index.ts'

const store = useStore()
const router = useRouter()

const info =ref({
    Uname:"",
    Upwd:""
})

const login = () => {
    if(info.value.Uname==""){
        alert("登录账号不能为空")
        return
    }
    if(info.value.Upwd==""){
        alert("登录密码不能为空")
        return
    }
    axios({
        url:"http://localhost:52342/api/User/UserLogin",
        method:"get",
        params:info.value
    })
    .then((res) => {
        if(res.data==-1){
            alert("用户不存在")
            return
        }else if(res.data==-2){
            alert("账号和密码不匹配")
            return
        }else if(res.data==0){
            alert("登录失败")
            return
        }else{
            alert("登录成功")
            store.Uid=res.data[0].Uid
            UserType(res.data[0].Uid)
        }
    })
    .catch((err) => {
        console.log(err)
    })
}

const UserType = (Uid:any) => {
    axios({
        url:"http://localhost:52342/api/User/UserType",
        method:"get",
        params:{
            Uid:Uid
        }
    })
    .then((res) => {
        if(res.data==-1){
            alert("未完善用户信息")
            return
        }
        if(res.data==1){
            router.push("/BorrowAdminShow")
        }else if(res.data==2){
            router.push("/BorrowUserShow")
        }
    })
    .catch((err) => {
        console.log(err)
    })
}




</script>


